﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

namespace Enter.Common
{
    public enum UploadUnitType
    {
        保存文件出错 = 4,
        成功 = 5,
        存储文件夹错误 = 1,
        图片格式错误 = 3,
        图片文件过大 = 2,
        无图片上传 = 6
    }
    public class UploadUnit
    {
        private bool _IsSuccess;
        private bool _IsSwf;
        private bool _IsThumbnail;
        private string _uploadFileExt;
        private string _uploadFormat;
        private int _uploadHigth;
        private int _uploadLength;
        private string _uploadPath;
        private UploadUnitType _uploadtype;
        private int _uploadWith;

        public UploadUnit()
        {
            this._uploadtype = UploadUnitType.成功;
            this._IsSuccess = false;
            this._uploadFileExt = string.Empty;
            this._uploadPath = string.Empty;
            this._uploadWith = 0;
            this._uploadFormat = "yyyyMMdd";
            this._IsThumbnail = true;
            this._IsSwf = false;
            this._uploadLength = 0xfa000;
            this._uploadHigth = 0;
        }

        public UploadUnit(string uploadPath)
        {
            this._uploadtype = UploadUnitType.成功;
            this._IsSuccess = false;
            this._uploadFileExt = string.Empty;
            this._uploadPath = string.Empty;
            this._uploadWith = 0;
            this._uploadFormat = "yyyyMMdd";
            this._IsThumbnail = true;
            this._IsSwf = false;
            this._uploadLength = 0xfa000;
            this._uploadHigth = 0;
            this._uploadPath = uploadPath;
        }

        private bool CheckFile()
        {
            if (!SafeCheck.IsMap(this._uploadPath))
            {
                this._uploadPath = FileHelper.GetFolderMapPath(this._uploadPath);
            }
            FileHelper.CreatedFolder(this._uploadPath);
            if (FileHelper.IsFolder(this._uploadPath))
            {
                return true;
            }
            this._uploadtype = UploadUnitType.存储文件夹错误;
            return false;
        }

        private bool CheckImg(HttpPostedFile fileUpload)
        {
            string fileName = fileUpload.FileName;
            if (!SafeCheck.IsJPGstr(fileName))
            {
                if (!this._IsSwf)
                {
                    this._uploadtype = UploadUnitType.图片格式错误;
                    return false;
                }
                if (!SafeCheck.IsSwfstr(fileName))
                {
                    this._uploadtype = UploadUnitType.图片格式错误;
                    return false;
                }
                this._IsThumbnail = false;
            }
            if (fileUpload.ContentLength == 0)
            {
                this._uploadtype = UploadUnitType.无图片上传;
                return false;
            }
            if (fileUpload.ContentLength > this._uploadLength)
            {
                this._uploadtype = UploadUnitType.图片文件过大;
                return false;
            }
            this._uploadFileExt = FileHelper.GetExtension(fileName).ToLower();
            return true;
        }
        private bool CheckImg(HttpPostedFileBase fileUpload)
        {
            string fileName = fileUpload.FileName;
            if (!SafeCheck.IsJPGstr(fileName))
            {
                if (!this._IsSwf)
                {
                    this._uploadtype = UploadUnitType.图片格式错误;
                    return false;
                }
                if (!SafeCheck.IsSwfstr(fileName))
                {
                    this._uploadtype = UploadUnitType.图片格式错误;
                    return false;
                }
                this._IsThumbnail = false;
            }
            if (fileUpload.ContentLength == 0)
            {
                this._uploadtype = UploadUnitType.无图片上传;
                return false;
            }
            if (fileUpload.ContentLength > this._uploadLength)
            {
                this._uploadtype = UploadUnitType.图片文件过大;
                return false;
            }
            this._uploadFileExt = FileHelper.GetExtension(fileName).ToLower();
            return true;
        }
        private string SaveImg(HttpPostedFile fileUpload)
        {
            try
            {
                int num = new Random().Next(100, 0x270f);
                string str2 = "00";
                string str3 = string.Format("{0}_{1}_{2}", DateTime.Now.ToString("HHmmss")+DateTime.Now.Millisecond.ToString(), str2, num.ToString());
                string folderData = string.Empty;
                string date = DateTime.Now.ToString(this._uploadFormat);
                string str6 = FileHelper.CreatedFolderData(this._uploadPath, date, out folderData);
                string str7 = folderData + str3 + this._uploadFileExt;
                fileUpload.SaveAs(this._uploadPath + str7);
                this._IsSuccess = true;
                if (this.IsThumbnail)
                {
                    return (folderData + str3 + this._uploadFileExt);
                }
                return str7;
            }
            catch
            {
                this._uploadtype = UploadUnitType.保存文件出错;
                return "";
            }
        }

        private string SaveImg(HttpPostedFile fileUpload, int imgType)
        {
            try
            {
                int num = new Random().Next(100, 0x270f);
                string str2 = string.Format("{0}_{1}_{2}", DateTime.Now.Millisecond.ToString(), imgType.ToString(), num.ToString());
                string folderData = string.Empty;
                string date = DateTime.Now.ToString(this._uploadFormat);
                string str5 = FileHelper.CreatedFolderData(this._uploadPath, date, out folderData);
                string str6 = folderData + str2 + this._uploadFileExt;
                fileUpload.SaveAs(this._uploadPath + str6);
                this._IsSuccess = true;
                if (this.IsThumbnail)
                {
                    return (folderData + str2 + this._uploadFileExt);
                }
                return str6;
            }
            catch
            {
                this._uploadtype = UploadUnitType.保存文件出错;
                return "";
            }
        }
       
        #region HttpPostedFileBase
        private string SaveImg(HttpPostedFileBase fileUpload, int imgType)
        {
            try
            {
                int num = new Random().Next(100, 0x270f);
                string str2 = string.Format("{0}_{1}_{2}", DateTime.Now.Millisecond.ToString(), imgType.ToString(), num.ToString());
                string folderData = string.Empty;
                string date = DateTime.Now.ToString(this._uploadFormat);
                string str5 = FileHelper.CreatedFolderData(this._uploadPath, date, out folderData);
                string str6 = folderData + str2 + this._uploadFileExt;
                fileUpload.SaveAs(this._uploadPath + str6);
                this._IsSuccess = true;
                if (this.IsThumbnail)
                {
                    return (folderData + str2 + this._uploadFileExt);
                }
                return str6;
            }
            catch
            {
                this._uploadtype = UploadUnitType.保存文件出错;
                return "";
            }
        }
       
        public string UpLoadImg(HttpPostedFileBase fileUpload, int imgType)
        {
            return this.UpLoadImg(fileUpload, 0x2710, 0x2710,imgType, out this._uploadtype);
        }
        public string UpLoadImg(HttpPostedFileBase fileUpload, int uploadWith, int uploadHigth, int imgType,out UploadUnitType uploadtype)
        {
            string str = string.Empty;
            this._uploadHigth = uploadHigth;
            this._uploadWith = uploadWith;
            if (this.CheckFile() && this.CheckImg(fileUpload))
            {
                str = this.SaveImg(fileUpload, imgType);
            }
            uploadtype = this._uploadtype;
            return str;
        }

        public string UpLoadImg(HttpPostedFileBase fileUpload, int uploadWith, int uploadHigth, out UploadUnitType uploadtype)
        {
            return this.UpLoadImg(fileUpload, uploadWith, uploadHigth, out uploadtype);
        }
     #endregion
        public string UpLoadImg(HttpPostedFile fileUpload)
        {
            return this.UpLoadImg(fileUpload, 0x2710, 0x2710, out this._uploadtype);
        }

        public string UpLoadImg(FileUpload fileUpload)
        {
            return this.UpLoadImg(fileUpload, 0x2710, 0x2710, out this._uploadtype);
        }
       
        public string UpLoadImg(HttpPostedFile fileUpload, out UploadUnitType uploadtype)
        {
            return this.UpLoadImg(fileUpload, 0x2710, 0x2710, out uploadtype);
        }

        public string UpLoadImg(FileUpload fileUpload, int imgType)
        {
            return this.UpLoadImg(fileUpload, 0x2710, 0x2710, out this._uploadtype, imgType);
        }

        public string UpLoadImg(FileUpload fileUpload, out UploadUnitType uploadtype)
        {
            return this.UpLoadImg(fileUpload, 0x2710, 0x2710, out uploadtype);
        }

        public string UpLoadImg(HttpPostedFile fileUpload, int uploadWith, int uploadHigth)
        {
            return this.UpLoadImg(fileUpload, 0x2710, 0x2710, out this._uploadtype);
        }

        public string UpLoadImg(FileUpload fileUpload, int uploadWith, int uploadHigth)
        {
            return this.UpLoadImg(fileUpload, 0x2710, 0x2710, out this._uploadtype);
        }

        public string UpLoadImg(HttpPostedFile fileUpload, int uploadWith, int uploadHigth, out UploadUnitType uploadtype)
        {
            string str = string.Empty;
            this._uploadHigth = uploadHigth;
            this._uploadWith = uploadWith;
            if (this.CheckFile() && this.CheckImg(fileUpload))
            {
                str = this.SaveImg(fileUpload);
            }
            uploadtype = this._uploadtype;
            return str;
        }

        public string UpLoadImg(FileUpload fileUpload, int uploadWith, int uploadHigth, out UploadUnitType uploadtype)
        {
            return this.UpLoadImg(fileUpload.PostedFile, uploadWith, uploadHigth, out uploadtype);
        }

        public string UpLoadImg(HttpPostedFile fileUpload, int uploadWith, int uploadHigth, out UploadUnitType uploadtype, int imgType)
        {
            string str = string.Empty;
            this._uploadHigth = uploadHigth;
            this._uploadWith = uploadWith;
            if (this.CheckFile() && this.CheckImg(fileUpload))
            {
                str = this.SaveImg(fileUpload, imgType);
            }
            uploadtype = this._uploadtype;
            return str;
        }

        public string UpLoadImg(FileUpload fileUpload, int uploadWith, int uploadHigth, out UploadUnitType uploadtype, int imgType)
        {
            return this.UpLoadImg(fileUpload.PostedFile, uploadWith, uploadHigth, out uploadtype, imgType);
        }
       
        public bool IsSuccess
        {
            get
            {
                return this._IsSuccess;
            }
            set
            {
                this._IsSuccess = value;
            }
        }

        public bool IsSwf
        {
            get
            {
                return this._IsSwf;
            }
            set
            {
                this._IsSwf = value;
            }
        }

        public bool IsThumbnail
        {
            get
            {
                return this._IsThumbnail;
            }
            set
            {
                this._IsThumbnail = value;
            }
        }

        public string UploadFormat
        {
            get
            {
                return this._uploadFormat;
            }
            set
            {
                this._uploadFormat = value;
            }
        }

        public int UploadHigth
        {
            get
            {
                return this._uploadHigth;
            }
            set
            {
                this._uploadHigth = value;
            }
        }

        public int UploadLength
        {
            get
            {
                return this._uploadLength;
            }
            set
            {
                this._uploadLength = value;
            }
        }

        public string UploadPath
        {
            get
            {
                return this._uploadPath;
            }
            set
            {
                this._uploadPath = value;
            }
        }

        public int UploadWith
        {
            get
            {
                return this._uploadWith;
            }
            set
            {
                this._uploadWith = value;
            }
        }
    }
}
